<!doctype html>
<html>

<head>
  <title>扯淡聊天室</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="index.css">
</head>

<body data-spy="scroll" data-target=".col-md-3">
  <div id="container">
    <div id="header">扯淡聊天室</div>
    <div id="page">
      <div class="row">
        <div class="col-md-9" id="messages">
          <script type="text/template" id="tpl">
            <% if (!isSelf){ %>
              <div class="media">
                <div class="media-left">
                  <span class="avatar" style="background-color: <%=user.color%>">hi</span>
                </div>
                <div class="media-body">
                  <p><b><%=user.name%></b> <span class="time"><%=time%></span></p>
                  <div class="alert alert-info" role="alert">
                    <%=msg%>
                  </div>
                </div>
              </div>
              <% }else{ %>
                <div class="media">
                  <div class="media-body">
                    <p class="text-right"><span class="time"><%=time%></span> <b><%=user.name%></b></p>
                    <div class="alert alert-info pull-right" role="alert">
                      <%=msg%>
                    </div>
                  </div>
                  <div class="media-right">
                    <span class="avatar" style="background-color: <%=user.color%>">hi</span>
                  </div>
                </div>
                <% } %>
          </script>
        </div>
        <div class="col-md-3 hidden-xs hidden-sm">
          <ul class="list-group" id="userlist" data-spy="affix" data-offset-top="60">
            <script type="text/template" id="usersTpl">
              <% _.each(users, function(v, i) { %>
                <li class="list-group-item">
                  <%=v.name%>
                </li>
                <% }) %>
            </script>
          </ul>
        </div>
      </div>
    </div>
    <div id="footer">
      <form>
        <input type="text" class="form-control" id="m" placeholder="支持markdown语法哦~~">
      </form>
    </div>
  </div>
  <script src="/socket.io/socket.io.js"></script>
  <script src="http://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
  <script src="http://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="http://cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
  <script>
  $(function() {
    var name = prompt("输入昵称");
    if ($.trim(name).length > 0) {
      var tplRender = _.template($("#tpl").html());
      var usersTplRender = _.template($("#usersTpl").html());
      var socket = io();
      socket.emit('user connection', name);
      $('form').submit(function() {
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(data) {
        if (data.user.id == '/#' + socket.id) {
          data.isSelf = true;
        } else {
          data.isSelf = false;
        }
        $('#messages').append(tplRender(data));
        $('html, body').animate({
          scrollTop: $(document).height()
        }, 0);
      });
      socket.on('hi', function(data) {
        $('#messages').append($('<p class="text-center">').text(data.msg));
        $("#userlist").html(usersTplRender(data));
        $('html, body').animate({
          scrollTop: $(document).height()
        }, 0);
      });
      $('form').show();
    }
  })
  </script>
</body>

</html>
